import UIKit
import Flutter
import Foundation


enum ChannelName {
  static let CUSTOMER_SHARE_SERVICE = "com.panda.customer.share"
  
}

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    //U-Common
    UMCommonSwiftInterface.initWithAppkey(appKey: "5df890fd4ca357a42400091c", channel:"appStore")
    // U-Share 平台设置
    UMSharePlatformSetting.configUSharePlatforms();
    UMSharePlatformSetting.confitUShareSettings();
    
    GeneratedPluginRegistrant.register(with: self)
    
    guard let controller = window?.rootViewController as? FlutterViewController else {
      fatalError("rootViewController is not type FlutterViewController")
    }
    let shareChannel = FlutterMethodChannel(name: ChannelName.CUSTOMER_SHARE_SERVICE,
                                            binaryMessenger: controller.binaryMessenger)
    shareChannel.setMethodCallHandler({
      (call: FlutterMethodCall, result: FlutterResult) -> Void in
        if(call.method == "shareUrl"){
            var type: Int = 0;
            var url: String = "";
            if(call.arguments != nil){
                let dict = call.arguments as? NSDictionary;
                if(dict != nil){
                    if dict?.value(forKey: "type") != nil {
                        type = dict?.value(forKey: "type") as! Int
                    }
                    if dict?.value(forKey: "type") != nil {
                        url = dict?.value(forKey: "url") as! String
                    }
                }
            }
            shareUrl(controller: controller, url: url, type: type);
            return
        } else {
            result(FlutterMethodNotImplemented)
            return
        }
    })
    
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

private func shareUrl(controller: UIViewController, url: String, type: Int){
    let urlObject = UMShareWebpageObject.shareObject(withTitle: "猿圈", descr: "程序猿的乐园", thumImage: nil)
    urlObject?.webpageUrl = url;
    guard let messageObject = UMSocialMessageObject.init(mediaObject: urlObject) else { return }
    var platformType = UMSocialPlatformType.unKnown
    
    switch type {
    case 1:
        platformType = UMSocialPlatformType.sina
        break;
    case 2:
        platformType = UMSocialPlatformType.wechatTimeLine
        break;
    case 3:
        platformType = UMSocialPlatformType.wechatSession
        break;
    case 4:
        platformType = UMSocialPlatformType.QQ
        break;
    case 5:
        break;
    case 8:
        ShareUtil.shareUtil.shareUrlBySystem(controller: controller, url: url);
        break;

      default:
        break;
    }
    
    if(type != 8){
        UMShareSwiftInterface.share(plattype: platformType, messageObject: messageObject, viewController: controller, completion:  { (shareResponse, error) in
            
        })
    }
    
}

